<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>承接页</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                </div>
                <div class="layui-card-body">
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="img">
                        <img src="/uploads/{{d.img}}" alt="">
                    </script>
                    <script type="text/html" id="form">
                        {{# switch(d.form){case 1: }}
                        单列表
                        {{# break;case 2: }}
                        双列表
                        {{# break;case 3: }}
                        自定义
                        {{# break;} }}
                    </script>
                    <script type="text/html" id="type">
                        {{# if (d.form == 4){ }}
                        自定义
                        {{# }else{ }}
                        {{# switch(d.type){case 1: }}
                        砍价
                        {{# break;case 2: }}
                        团购
                        {{# break;case 3: }}
                        秒杀
                        {{# break;case 4: }}
                        全返
                        {{# break;case 5: }}
                        N元购
                        {{# break;case 6: }}
                        点赞
                        {{# break;case 7: }}
                        优惠券商品
                        {{# break;case 8: }}
                        自选商品
                        {{# break;} }}
                        {{# } }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$,
            form = layui.form,
            table = layui.table;

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('System/content_page')}",
            method: 'post',
            cols: [[
                    {field: 'name', title: '页面名称'},
                    {field: 'form', title: '展现形式', templet: "#form"},
                    {field: 'type', title: '调用数据', templet: "#type"},
                    {field: 'num', title: '调用条数', templet: function (d){ return d.num ? d.num + '条' : '-'; }},
                    {fixed: 'right', title: '操作', align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if(layEvent === 'edit'){ //编辑
                parent.layer.open({
                    type: 2,
                    title: '编辑承接页',
                    content: '/admin/System/content_page_operate/id/' + id,
                    maxmin: true,
                    area: ['80%', '90%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index) {
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            } else if(layEvent === 'del'){ //删除
                parent.layer.confirm('确定删除吗', function(index){
                    //向服务端发送删除指令
                    $.ajax({
                        type: "POST",
                        url: "{:url('System/content_page_del')}",
                        data: {id:id},
                        dataType: "json",
                        success: function(e){
                            if (e.code == 200){
                                table.reload('demo');
                                parent.layer.msg(e.data);
                            }else{
                                parent.layer.msg(e.msg);
                            }
                        }
                    });
                });
            }
        });

        //事件
        var active = {
            add: function(){
                parent.layer.open({
                    type: 2,
                    title: '添加承接页',
                    content: "{:url('System/content_page_operate')}",
                    maxmin: true,
                    area: ['80%', '90%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    success: function (layero, index) {
                        // 获取子页面的iframe
                        var iframe = parent['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        iframe.child(window);
                    },
                    yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
